//package com.hitqz.robot.biz.controller;
//
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.hitqz.robot.api.business.enums.BrandEnum;
//import com.hitqz.robot.api.business.enums.EquipmentEnum;
//import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
//import com.hitqz.robot.api.common.dto.robot.RobotMoveDto;
//import com.hitqz.robot.api.common.entity.RobotEquipment;
//import com.hitqz.robot.biz.service.RobotEquipmentService;
//import com.hitqz.robot.camera.EquipmentFactory;
//import com.hitqz.robot.camera.IEquipmentService;
//import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
//import com.hitqz.robot.camera.dto.QueryParamsDto;
//import com.hitqz.robot.camera.dto.WriteSpeedParamsDto;
//import com.hitqz.robot.common.core.util.R;
//import com.hitqz.robot.driver.parent.RobotDriverService;
//import io.swagger.v3.oas.annotations.tags.Tag;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//
//import java.io.DataOutputStream;
//import java.net.Socket;
//
///**
// * @author xupkun
// * @date 2024/12/10
// */
//@RestController
//@AllArgsConstructor
//@RequestMapping("/test")
//@Tag(name = "测试")
//@Slf4j
//public class TestController {
//
//    private final RobotDriverService robotDriverService;
//
//    private final RobotEquipmentService robotEquipmentService;
//
//    private final EquipmentFactory equipmentFactory;
//
//    @GetMapping(value = "test")
//    public R test(@RequestParam(value = "speed") String speed) throws InterruptedException {
//        robotDriverService.executeOtherCommand(RobotOtherCommandEnum.ROD_CONTROLLER, "");
//        Thread.sleep(1000);
//        Thread thread = new Thread(() -> {
//            while (true) {
//                try {
//                    Thread.sleep(250);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//                log.info("set speed");
//                setSpeed("0.01", "0", null);
//            }
//        });
//        thread.start();
//        Thread.sleep(500);
//        setSpeed("0", "0", "0");
//        log.info("thread run stopped");
//        thread.stop();
//        return R.ok();
//    }
//
//
//    private void setSpeed(String linerSpeed, String linerSpeedY, String angularSpeed) {
//        RobotMoveDto robotMoveDto = new RobotMoveDto();
//        robotMoveDto.setLinerSpeed(linerSpeed);
//        robotMoveDto.setLinerSpeedY(linerSpeedY);
//        robotMoveDto.setAngularSpeed(angularSpeed);
//        robotDriverService.setSpeed(robotMoveDto);
//    }
//
//
//    @GetMapping(value = "plcStatus")
//    public R plcStatus() throws InterruptedException {
//        RobotEquipment robotEquipment = robotEquipmentService.getOne(Wrappers.<RobotEquipment>lambdaQuery()
//                .eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
//                .eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
//                .eq(RobotEquipment::getBrand, BrandEnum.CAR_PLC.getEnStr())
//                .eq(RobotEquipment::getIsRecordParam, 1));
//        IEquipmentService iEquipmentService = equipmentFactory.getServiceByBrand(robotEquipment.getBrand());
//        QueryParamsDto queryParamsDto = new QueryParamsDto();
//        queryParamsDto.setPassword(robotEquipment.getPassword());
//        queryParamsDto.setUsername(robotEquipment.getUsername());
//        queryParamsDto.setPort(robotEquipment.getPort());
//        queryParamsDto.setChannel(robotEquipment.getChannel());
//        queryParamsDto.setIp(robotEquipment.getIp());
//
//        return R.ok(iEquipmentService.getEquipmentParams(queryParamsDto));
//    }
//
//
//    @GetMapping(value = "openOrClose")
//    public R openOrClose(String cmd) {
//        robotDriverService.executeOtherCommand(RobotOtherCommandEnum.AVOID_SWITCH, cmd);
//        return R.ok();
//    }
//
//
//    @GetMapping(value = "changeSpeed")
//    public R getControlButtonList(@RequestParam(value = "changeValue") int changeValue) {
//        RobotEquipment robotEquipment = robotEquipmentService.getOne(Wrappers.<RobotEquipment>lambdaQuery()
//                .eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
//                .eq(RobotEquipment::getBrand, BrandEnum.CAR_PLC.getEnStr())
//                .eq(RobotEquipment::getIsRecordParam, 1));
//        IEquipmentService iEquipmentService = equipmentFactory.getServiceByBrand(robotEquipment.getBrand());
//        QueryParamsDto queryParamsDto = new QueryParamsDto();
//        queryParamsDto.setPassword(robotEquipment.getPassword());
//        queryParamsDto.setUsername(robotEquipment.getUsername());
//        queryParamsDto.setPort(robotEquipment.getPort());
//        queryParamsDto.setChannel(robotEquipment.getChannel());
//        queryParamsDto.setIp(robotEquipment.getIp());
//        WriteSpeedParamsDto writeSpeedDto = new WriteSpeedParamsDto();
//        writeSpeedDto.setSpeed(changeValue);
//        iEquipmentService.executeCmd(queryParamsDto, false, EquipmentCmdEnum.WRITE_SPEED, writeSpeedDto);
//        return R.ok();
//    }
//
//
//    @GetMapping(value = "open")
//    public R open(String ip) {
//
//        byte[] close1 = new byte[] {
//                (byte) 0xFE, 0x05, 0x00,0x00, (byte) 0x00, 0x00, (byte) 0xD9, (byte) 0xC5
//        };
//        byte[] open2 = new byte[] {
//                (byte)0xFE, 0x05, 0x00, 0x01,  (byte)0xFF, 0x00, (byte) 0xC9, (byte) 0xF5
//        };
//        byte[] open3 = new byte[] {
//                (byte) 0xFE, 0x05, 0x00, 0x02, (byte) 0xFF, 0x00, 0x39, (byte) 0xF5
//        };
//
//        byte[] open4 = new byte[] {
//                (byte) 0xFE, 0x05, 0x00, 0x03, (byte) 0xFF, 0x00, 0x68, (byte) 0x35
//        };
//
//        byte[] close2 = new byte[] {
//                (byte) 0xFE, 0x05, 0x00 , 0x01 , 0x00 , 0x00 , (byte) 0x88, 0x05
//        };
//
//        send(ip, close1);
//        //TimeUnit.MILLISECONDS.sleep(500);
//        send(ip, open2);
//        //TimeUnit.MILLISECONDS.sleep(500);
//        send(ip, open3);
//        //TimeUnit.MILLISECONDS.sleep(500);
//        send(ip, open4);
//        //TimeUnit.MILLISECONDS.sleep(500);
//        //send(ip, close2);
//
//        return R.ok();
//    }
//    @GetMapping(value = "close")
//    public R close(String ip) {
//
//        byte[] close2 = new byte[]{
//                (byte) 0xFE, 0x05, 0x00, 0x01, 0x00, 0x00, (byte) 0x88, 0x05
//        };
//        byte[] close3 = new byte[]{
//                (byte) 0xFE, 0x05, 0x00, 0x02, 0x00, 0x00, 0x78, 0x05
//        };
//        byte[] close4 = new byte[]{
//                (byte) 0xFE, 0x05, 0x00, 0x03, 0x00, 0x00, 0x29, (byte) 0xC5
//        };
//        byte[] open1 = new byte[]{
//                (byte) 0xFE, 0x05, 0x00, 0x00, (byte) 0xFF, 0x00, (byte) 0x98, 0x35
//        };
//        byte[] close1 = new byte[]{
//                (byte) 0xFE, 0x05, 0x00, 0x00, (byte) 0x00, 0x00, (byte) 0xD9, (byte) 0xC5
//        };
//        //TimeUnit.MILLISECONDS.sleep(500);
//        send(ip, close2);
//        //TimeUnit.MILLISECONDS.sleep(500);
//        send(ip, open1);
//        //TimeUnit.MILLISECONDS.sleep(500);
//        send(ip, close3);
//        //TimeUnit.MILLISECONDS.sleep(500);
//        send(ip, close4);
//        //TimeUnit.MILLISECONDS.sleep(500);
//        //send(ip, close1);
//
//        return R.ok();
//    }
//
//
//    public void send(String ip, byte[] data) {
//        try {
//            int port = 10000;
//            Socket socket = new Socket(ip, port);
//            socket.setSoTimeout(60000);
//            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
//            out.write(data);
//            out.close();
//            socket.close();
//        } catch (Exception e) {
//
//        }
//    }
//
//}
